package com.is.control;

import java.sql.ResultSet;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.is.dbconn.DBConnect;
import com.is.model.ParamStuyear;
import com.is.util.DateTime;
public class ParamStuyearControl {
    /*
     * 添加学年
     */
    public static boolean insertParamStuyear(ParamStuyear stu) {
	DBConnect dbc = new DBConnect();
	String sql = "insert into param_stuyear (stuyear,startTime,endTime,typeId)values(?,?,?,?)";
	dbc.prepareStatement(sql);
	try {
	    dbc.setString(1, stu.getStuyear());
	    dbc.setString(2, stu.getStartTime());
	    dbc.setString(3, stu.getEndTime());
	    dbc.setInt(4, stu.getTypeId());
	    dbc.executeUpdate();
	    return true;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("插入失败！");
	    return false;
	}finally{
	    dbc.close();
	}
    }
    
    /*
     * 根据id查询某一学年；
     */
    public static ParamStuyear getStuyear(int id) {
	DBConnect dbc = new DBConnect();
	ParamStuyear stuyear = null;
	String sql = "select * from param_stuyear where id = "+id;
	dbc.prepareStatement(sql);
	ResultSet rs = null;
	try {
	    rs = dbc.executeQuery();
	    if(rs.next()){
		stuyear = new ParamStuyear();
		stuyear.setId(id);
		stuyear.setStuyear(rs.getString("stuyear"));
		stuyear.setStartTime(rs.getString("startTime"));
		stuyear.setEndTime(rs.getString("endTime"));
		stuyear.setTypeId(rs.getInt("typeId"));
		return stuyear;
	    }
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("查找学年失败！");
	    return null;
	}finally{
	    dbc.close();
	}
	return null;
    }
    
    /*
     * 遍历所有学年（供查询时选择）
     */
    public static List<ParamStuyear> getAllStuyear() {
	DBConnect dbc = new DBConnect();
	List<ParamStuyear> sylist = new ArrayList<ParamStuyear>();
	ParamStuyear sy = null;
	String sql = "select * from param_stuyear order by id desc"; 
	dbc.prepareStatement(sql);
	ResultSet rs = null;
	try {
	    rs = dbc.executeQuery();
	    while(rs.next()){
		sy = new ParamStuyear();
		sy.setId(rs.getInt("id"));
		sy.setStuyear(rs.getString("stuyear"));
		sy.setStartTime(rs.getString("startTime"));
		sy.setEndTime(rs.getString("endTime"));
		sy.setTypeId(rs.getInt("typeId"));
		sylist.add(sy);
	    }
	    return sylist;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("遍历学年失败！");
	    return null;
	}finally{
	    dbc.close();
	}
    }
    
    /*
     * 根据时间判断所在学期
     */
    public static int getStuyearIdByTime(String currenttime) throws ParseException{
	DBConnect dbc = new DBConnect();
	ResultSet rs =null;
	int stuyearid=0;
	String sql ="select id from param_stuyear where startTime <='"+currenttime+"' and endTime >= '"+currenttime+"'";
	dbc.prepareStatement(sql);
	try {
	     rs = dbc.executeQuery();
	    if(rs.next())
		stuyearid = rs.getInt("id");
	    return stuyearid;
		
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("查找stuyearid失败!");
	    return 0;
	}finally{
	    dbc.close();
	}
	
    }
    
    
    /*
     * 获取当前所在学期
     */
    public static int getCurrentStuyearId() throws ParseException{
	return getStuyearIdByTime(DateTime.SimpleDate());
    }
    
    public static ParamStuyear getCurrentStuyear() throws ParseException{
    	return getStuyear(getCurrentStuyearId());
    }
   
    /**
     * 获取当前正在学习的年级
     * @param args
     */
//    public static int getCurrentStudentGrade(){
//	int now = DateTime.dateToInt();
//	int year = DateTime.getYear();
//	if(year*10000+815>now){
//	    return year-1;
//	}else{
//	    return year;
//	}
//    }
    
   
    /**
     * 获取当前处于第一学期还是第二学期
     * @param args
     */
//    public static String getCurrentStudyPart(){
//	int now = DateTime.dateToInt();
//	//System.out.println(now);
//	int year = DateTime.getYear();
//	//System.out.println(year);
//	if(year*10000+815>now&&now>year*10000+220){
//	    return "2";
//	}else{
//	    return "1";
//	}
//    } 
//    
//    public static void main(String args[]) throws ParseException {
//	System.out.println(getCurrentStudyPart());
//    }
    

}
